package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.exception.BusinessException;
import com.example.domain.CultureProductCollect;
import com.example.mapper.CultureProductCollectDao;
import com.example.service.CultureProductCollectService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (CultureProductCollect)表服务实现类
 *
 * @author lidongsheng
 * @since 2025-03-30 14:50:23
 */
@Service("cultureProductCollectService")
public class CultureProductCollectServiceImpl implements CultureProductCollectService {
    @Resource
    private CultureProductCollectDao cultureProductCollectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CultureProductCollect queryById(Integer id) {
        return this.cultureProductCollectDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param cultureProductCollect 筛选条件
     * @param pageRequest           分页对象
     * @return 查询结果
     */
    @Override
    public Page<CultureProductCollect> queryByPage(CultureProductCollect cultureProductCollect, PageRequest pageRequest) {
        long total = this.cultureProductCollectDao.count(cultureProductCollect);
        return new PageImpl<>(this.cultureProductCollectDao.queryAllByLimit(cultureProductCollect, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param cultureProductCollect 实例对象
     * @return 实例对象
     */
    @Override
    public CultureProductCollect insert(CultureProductCollect cultureProductCollect) {
        CultureProductCollect cultureProductCollect1 = cultureProductCollectDao.queryCollectByUserAndProduct(cultureProductCollect);
        if (ObjectUtil.isNotEmpty(cultureProductCollect1)) {
            throw new BusinessException(ResultCodeEnum.COLLECT_ERROR);
        }
        this.cultureProductCollectDao.insert(cultureProductCollect);
        return cultureProductCollect;
    }

    /**
     * 修改数据
     *
     * @param cultureProductCollect 实例对象
     * @return 实例对象
     */
    @Override
    public CultureProductCollect update(CultureProductCollect cultureProductCollect) {
        this.cultureProductCollectDao.update(cultureProductCollect);
        return this.queryById(cultureProductCollect.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer productId,Integer userId) {
        return this.cultureProductCollectDao.deleteById(productId,userId) > 0;
    }
}
